package net.thermetics.fx.model;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;

import net.thermetics.fx.auth.GaeUser;
import net.thermetics.fx.services.UserService;

import org.springframework.beans.factory.annotation.Autowired;

import com.google.appengine.api.datastore.Key;

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
public class ReportVote extends ModelObject {
	private static final long serialVersionUID = 1L;
	
	public static final String FLAMING = "Flaming";
	public static final String CAMPAIGNING = "Campaigning";
	public static final String NO_VIOLATION = "No Violation";
	
	public static final String DELETE = "Delete";
	public static final String EDIT = "Edit";
	
	@Autowired
	private UserService userService;
	
	@Persistent
	private String violation;
	
	@Persistent
	private String action;
	
	@Persistent
	private Key userKey;
	
	@Persistent
	private String comment;
	
	public String getViolation() {
		return violation;
	}
	
	public void setViolation(String violation) {
		this.violation = violation;
	}
	
	public String getAction() {
		return action;
	}
	
	public void setAction(String action) {
		this.action = action;
	}

	public void setUserKey(Key userKey) {
		this.userKey = userKey;
	}

	public Key getUserKey() {
		return userKey;
	}
	
	public GaeUser getUser() {
		return userService.getUser(getUserKey());
	}
	
	public String getComment() {
		return comment;
	}
	
	public void setComment(String comment) {
		this.comment = comment;
	}
	
	public List<String> getViolationList() {
		if (getViolation() == null) {
			return new ArrayList<String>();
		}
		
		List<String> violations = new ArrayList<String>();
		
		String[] arr = getViolation().split(",");
		
		for (String vio : arr) {
			violations.add(vio.trim());
		}
		
		return violations;
	}
	
	public String toString() {
		if (getViolation().equals(NO_VIOLATION)) {
			return NO_VIOLATION;
		}
		else {
			return getAction() + " &44;" + getViolation();
		}
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((userKey == null) ? 0 : userKey.hashCode());
		return result;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ReportVote other = (ReportVote) obj;
		if (userKey == null) {
			if (other.userKey != null)
				return false;
		} else if (!userKey.equals(other.userKey))
			return false;
		return true;
	}
}
